﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenComposite.HumanActivity.Contracts;
using OpenComposite.HumanActivity.Data;
using System.ServiceModel;

namespace OpenComposite.HumanActivity.Services
{
	[ServiceBehavior(
		ConcurrencyMode = ConcurrencyMode.Multiple,
		InstanceContextMode = InstanceContextMode.Single,
		Namespace = "http://www.theopencomposite.com/internal")]
	public class AttachmentService : IAttachmentService
	{
		#region IAttachmentService Members

		public bool DeleteAttachment(LogInData userdata, int attachmentId)
		{
			return Global.DataAccess.DeleteAttachment(attachmentId, userdata.UserID.Value);
		}

		public byte[] GetAttachmentData(LogInData userdata, int attachmentId)
		{
			return Global.DataAccess.GetAttachmentData(attachmentId);
		}

		public List<AttachmentItem> GetAttachmentList(LogInData userdata, Guid workflowInstanceId)
		{
			return Global.DataAccess.GetAttachmentsList(workflowInstanceId);
		}

		public bool SetAttachment(LogInData userdata, AttachmentItem attachment)
		{
			if ( attachment.AttachmentId.HasValue ) {
				return Global.DataAccess.UpdateAttachment(
					attachment.AttachmentId.Value,
					attachment.Name,
					attachment.Description,
					attachment.FileName,
					attachment.Data,
					attachment.LastModifiedById);
			} else {
				int attachmentId;
				return Global.DataAccess.AddAttachment(
					attachment.WorkflowInstanceId,
					attachment.ActivityId,
					attachment.Name,
					attachment.Description,
					attachment.FileName,
					attachment.Data,
					attachment.CreatedById,
					out attachmentId);
			}
		}

		#endregion
	}
}
